package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/20.
 */

public class ReceiveUtils {
    //获取货品信息
    public static ReceiveReceiveItemBean getReceiveReceiveItemBean(Context context) {
        String json = SPUtils.getReceiveInformation(context);
        Gson gson = new Gson();
        return gson.fromJson(json, ReceiveReceiveItemBean.class);
    }

    //添加验收人
    public static void addReceiver(Context context, ReceiveReceiveItemBean.DataBean.ReceiverListBean receiveItem) {
        ReceiveReceiveItemBean bean = getReceiveReceiveItemBean(context);
        List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> receiverList = bean.getData().getReceiverList();
        receiverList.add(0, receiveItem);
        bean.getData().setReceiverList(receiverList);
        Gson gson = new Gson();
        String json = gson.toJson(bean, ReceiveReceiveItemBean.class);
        SPUtils.saveReceiveInformation(context, json);
    }

    //删除验收人
    public static void deleteReceiver(Context context, String id) {
        ReceiveReceiveItemBean bean = getReceiveReceiveItemBean(context);
        List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> receiverList = bean.getData().getReceiverList();
        for (int i = 0; i < receiverList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.ReceiverListBean receiverItem = receiverList.get(i);
            if (receiverItem.getId().equals(id)) {
                receiverList.remove(receiverItem);
                break;
            }
        }
        bean.getData().setReceiverList(receiverList);
        Gson gson = new Gson();
        String json = gson.toJson(bean, ReceiveReceiveItemBean.class);
        SPUtils.saveReceiveInformation(context, json);
    }

    //获取货品列表信息
    public static List<ReceiveReceiveItemBean.DataBean.GoodsListBean> getGoodsList(Context context, String goodsNumber) {
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList = new ArrayList<>();
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> allGoodsList = getReceiveReceiveItemBean(context).getData().getGoodsList();
        for (int i = 0; i < allGoodsList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem = allGoodsList.get(i);
            if (goodsItem.getMaterielDescribe().contains(goodsNumber) || goodsItem.getMateriel().contains(goodsNumber)) {
                goodsList.add(goodsItem);
                continue;
            }
        }
        return goodsList;
    }

    //获取不合格处理方式
    public static List<ReceiveReceiveItemBean.DataBean.DisposeWayListBean> getDisposeWayList(Context context) {
        return getReceiveReceiveItemBean(context).getData().getDisposeWayList();
    }

    public static ReceiveReceiveItemBean.DataBean.GoodsListBean getGoodsInformationBySerial(Context context, String serial) {
        ReceiveReceiveItemBean bean = getReceiveReceiveItemBean(context);
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList = bean.getData().getGoodsList();
        for (int i = 0; i < goodsList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goods = goodsList.get(i);
            if (goods.getSerialNumber().equals(serial)) {
                return goods;
            }
        }
        return null;
    }

    public static void updateGoodsItem(Context context, ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItems) {
        ReceiveReceiveItemBean bean = getReceiveReceiveItemBean(context);
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList = bean.getData().getGoodsList();
        for (int i = 0; i < goodsList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goods = goodsList.get(i);
            if (goods.getSerialNumber().equals(goodsItems.getSerialNumber())) {
                goodsList.remove(goods);
                break;
            }
        }
        goodsList.add(0, goodsItems);
        bean.getData().setGoodsList(goodsList);
        Gson gson = new Gson();
        String json = gson.toJson(bean, ReceiveReceiveItemBean.class);
        SPUtils.saveReceiveInformation(context, json);
    }

    public static ReceiveConfirmRequestBean getReceiveConfirmRequestBean(Context context, boolean isSaveCommit) {
        ReceiveConfirmRequestBean result = new ReceiveConfirmRequestBean();
        ReceiveReceiveItemBean bean = getReceiveReceiveItemBean(context);
        result.setDeliveryNumber(bean.getData().getDeliveryNumber());
        result.setReceiveNumber(bean.getData().getReceiveNumber());
        result.setIsSaveCommit(isSaveCommit);
        List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> receiverList = bean.getData().getReceiverList();
        List<ReceiveConfirmRequestBean.ReceiverListBean> receiverResultList = new ArrayList<>();
        for (int i = 0; i < receiverList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.ReceiverListBean receiverItem = receiverList.get(i);
            ReceiveConfirmRequestBean.ReceiverListBean receiverItemResult = new ReceiveConfirmRequestBean.ReceiverListBean();
            receiverItemResult.setId(receiverItem.getId());
            receiverResultList.add(receiverItemResult);
        }
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList = bean.getData().getGoodsList();
        List<ReceiveConfirmRequestBean.GoodsListBean> goodsResultList = new ArrayList<>();
        result.setReceiverList(receiverResultList);
        for (int i = 0; i < goodsList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem = goodsList.get(i);
            ReceiveConfirmRequestBean.GoodsListBean goodsItemResult = new ReceiveConfirmRequestBean.GoodsListBean();
            goodsItemResult.setSerial(goodsItem.getSerialNumber());
            goodsItemResult.setReceiveNumber(goodsItem.getReceiveNum());
            goodsItemResult.setReceiveDate(goodsItem.getReceiveDate());
            goodsItemResult.setQualifiedNumber(goodsItem.getQualifiedNumber());
            goodsItemResult.setCodeValue(goodsItem.getCodeValue());
            goodsResultList.add(goodsItemResult);
        }
        result.setGoodsList(goodsResultList);
        return result;
    }

    public static boolean hasReceiveNumber(Context context) {
        List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList = getReceiveReceiveItemBean(context).getData().getGoodsList();
        for (int i = 0; i < goodsList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem = goodsList.get(i);
            if (!"".equals(goodsItem.getReceiveNum())) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasThreeReceiver(Context context) {
        List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> receiverList = getReceiveReceiveItemBean(context).getData().getReceiverList();
        if (receiverList != null && !receiverList.isEmpty() && receiverList.size() >= 3) {
            return true;
        }
        return false;
    }

    public static ReceiveReceiveItemBean.DataBean.DisposeWayListBean getDefaultDisposeWay(Context context) {
        List<ReceiveReceiveItemBean.DataBean.DisposeWayListBean> disposeWayList = getReceiveReceiveItemBean(context).getData().getDisposeWayList();
        return disposeWayList.get(0);
    }

    public static boolean checkGoodsItem(ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem) {
        if (TextUtils.isEmpty(goodsItem.getReceiveDate()) || TextUtils.isEmpty(goodsItem.getReceiveNum())) {
            return false;
        } else {
            return true;
        }
    }

    public static List<OverviewAdapter.OverviewBean> getOverViewBeanWithGoodsList(List<ReceiveReceiveItemBean.DataBean.GoodsListBean> materielList) {
        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
        for (int i = 0; i < materielList.size(); i++) {
            ReceiveReceiveItemBean.DataBean.GoodsListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getReceiveNum())) {
                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
                overItem.setInfo(materielItem.getMaterielDescribe());
                overItem.setNumber(materielItem.getReceiveNum() + materielItem.getUnit());
                resultList.add(overItem);
            }
        }
        return resultList;
    }
}
